package Executor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class Main {
	public static void main(String[] args){
		XPathMinimizer xpmini = new XPathMinimizer("auction.xml");
		testcase1(xpmini);
		
		testcase2(xpmini);
		testcase2a(xpmini);
		
		testcase3(xpmini);
		testcase3a(xpmini);
		
		testcase4(xpmini);
		testcase4a(xpmini);
		
		testcase5(xpmini);
		testcase5a(xpmini);
		
		testcase6(xpmini);
		
	}

	

	private static void testcase6(XPathMinimizer xpmini) {
		String query = "//a[/b[c//d//f]/c/e[h and //g]]/b/c/e";
		
		HashMap<String,String> parent = new HashMap<String,String>();
		parent.put("h", "e");
		parent.put("g", "e");
		
		HashMap<String,List<String>> ancestor = new HashMap<String,List<String>>() ;
		List<String> ancd = new ArrayList<String>();
		ancd.add("e");		
		ancestor.put("d", ancd);
		List<String> ancf = new ArrayList<String>();
		ancf.add("d");
		ancestor.put("f", ancf);
		xpmini.setConstrains(parent, ancestor);
		
		xpmini.executeQuery(query,true);
		System.out.println(xpmini.getProcessedQuery());
		System.out.println("Query minimization time : "+xpmini.getQueryMinimizationTime());
		System.out.println("Minimized query execution time : "+xpmini.getExecutionTimeMinimizedQuery());
		System.out.println(xpmini.getQueryResult());	
		
	}
	
	private static void testcase5a(XPathMinimizer xpmini) {
		//String query = "//open_auctions[open_auction//increase]/open_auction[bidder//increase]";
		String query = "//africa//item[mailbox//text]/mailbox[mail//text]";
		xpmini.executeQuery(query,false);
		System.out.println(xpmini.getExecutionTimeOriginalQuery());
		
		//System.out.println(xpmini.getQueryResult());			
	}
	private static void testcase5(XPathMinimizer xpmini) {
		String query = "//africa//item[mailbox//text]/mailbox[mail//text]";
		HashMap<String,String> parent = new HashMap<String,String>();
		HashMap<String,List<String>> ancestor = new HashMap<String,List<String>>() ;
		xpmini.setConstrains(parent, ancestor);
		
		
		xpmini.executeQuery(query,true);
		
		System.out.println("Query minimization time : "+xpmini.getQueryMinimizationTime());
		System.out.println("Minimized query execution time : "+xpmini.getExecutionTimeMinimizedQuery());
		
		 
		//System.out.println(xpmini.getQueryResult());			
	}

	private static void testcase4(XPathMinimizer xpmini) {
		String query = "//regions[//item[location and quantity and shipping]]//name";
		HashMap<String,String> parent = new HashMap<String,String>();
		parent.put("location", "item");
		parent.put("quantity", "item");
		parent.put("shipping", "item");
		HashMap<String,List<String>> ancestor = new HashMap<String,List<String>>() ;
		List<String> anc = new ArrayList<String>();
		anc.add("regions");
		ancestor.put("item", anc);
		xpmini.setConstrains(parent, ancestor);
		
		xpmini.executeQuery(query, false);
		xpmini.executeQuery(query, false);
		System.out.println(xpmini.getExecutionTimeOriginalQuery());
		xpmini.executeQuery(query,true);
		xpmini.executeQuery(query, true);
		//System.out.println("Query minimization time : "+xpmini.getQueryMinimizationTime());
		System.out.println(xpmini.getExecutionTimeMinimizedQuery());
		
		//System.out.println(xpmini.getQueryResult());	
		
	}
	private static void testcase4a(XPathMinimizer xpmini) {
		String query = "//regions[//item[location and quantity and shipping]]//name";
		HashMap<String,String> parent = new HashMap<String,String>();
		parent.put("location", "item");
		parent.put("quantity", "item");
		parent.put("shipping", "item");
		HashMap<String,List<String>> ancestor = new HashMap<String,List<String>>() ;
		List<String> anc = new ArrayList<String>();
		anc.add("regions");
		ancestor.put("item", anc);
		xpmini.setConstrains(parent, ancestor);
		
		xpmini.executeQuery(query,false);
		System.out.println(xpmini.getExecutionTimeOriginalQuery());
		
		
	}

	private static void testcase3(XPathMinimizer xpmini) {
		String query = "//people[//watches/watch]/person/name";
		HashMap<String,String> parent = new HashMap<String,String>();
		parent.put("watch", "watches");
		parent.put("name", "person");
		HashMap<String,List<String>> ancestor = new HashMap<String,List<String>>() ;
		xpmini.setConstrains(parent, ancestor);
		xpmini.executeQuery(query,true);
		xpmini.executeQuery(query, true);
		//System.out.println("Query minimization time : "+xpmini.getQueryMinimizationTime());
		System.out.println(xpmini.getExecutionTimeMinimizedQuery());
		xpmini.executeQuery(query, false);
		xpmini.executeQuery(query, false);
		System.out.println(xpmini.getExecutionTimeOriginalQuery());
		
		//if (queryOri.equals(xpmini.getQueryResult())) System.out.println("true");
		//System.out.println(xpmini.getQueryResult());	
		
	}
	private static void testcase3a(XPathMinimizer xpmini) {
		String query = "//people[//watches/watch]/person/name";
		
		
		xpmini.executeQuery(query,false);
		System.out.println(xpmini.getExecutionTimeOriginalQuery());

		
		
		//System.out.println(xpmini.getQueryResult());	
		
	}

	private static void testcase2(XPathMinimizer xpmini) {
		String query = "//closed_auction[annotation//text]/annotation[description]";
		HashMap<String,String> parent = new HashMap<String,String>();
		HashMap<String,List<String>> ancestor = new HashMap<String,List<String>>() ;
		List<String> anc = new ArrayList<String>();
		anc.add("description");
		ancestor.put("text", anc);
		xpmini.setConstrains(parent, ancestor);
		
		xpmini.executeQuery(query,true);
		
		System.out.println("Query minimization time : "+xpmini.getQueryMinimizationTime());
		System.out.println("Minimized query execution time : "+xpmini.getExecutionTimeMinimizedQuery());
		//if (queryResult.equals(xpmini.getQueryResult()))System.out.println("true");
		//System.out.println(xpmini.getQueryResult());	
		
	}
	
	private static void testcase2a(XPathMinimizer xpmini) {
		String query = "//closed_auction[annotation//text]/annotation[description]";
		
		xpmini.executeQuery(query,false);
		
		
		System.out.println(xpmini.getExecutionTimeOriginalQuery());
		
		//if (queryResult.equals(xpmini.getQueryResult()))System.out.println("true");
		//System.out.println(xpmini.getQueryResult());	
		
	}


	private static void testcase1(XPathMinimizer xpmini) {
		String query = "//book|//author";
		xpmini.executeQuery(query,false);
		
		System.out.println(xpmini.getExecutionTimeOriginalQuery());
		xpmini.executeQuery(query,true);
		System.out.println("Query minimization time : "+xpmini.getQueryMinimizationTime());
		System.out.println("Minimized query execution time : "+xpmini.getExecutionTimeMinimizedQuery());
		System.out.println(xpmini.getQueryResult());	
	}
}
